﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLTV
{
    public partial class frmManHinhChinh : Form
    {
        public frmManHinhChinh()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ChuaDangNhap();
        }        

        private void QuanLyDocGiaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmQuanLyDocGia frmDG = new frmQuanLyDocGia();
            frmDG.MdiParent = this;
            frmDG.Show();
        }

        private void loạiĐộcGiảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLoaiDocGia frmLDG = new frmLoaiDocGia();
            frmLDG.Show();
        }

        private void NhanTraSachToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmPhieuTraSach frmPhieuTra = new frmPhieuTraSach();
            frmPhieuTra.MdiParent = this;
            frmPhieuTra.Show();
        }

        private void sáchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmTraCuuSach frmTraCuu = new frmTraCuuSach();
            frmTraCuu.MdiParent = this;
            frmTraCuu.Show();
        }

        private void ChuaDangNhap()
        {
            //----Quản Lý----
            QuanLyNhanVienToolStripMenuItem.Enabled = false;
            QuanLyDocGiaToolStripMenuItem.Enabled = false;
            QuanLySachToolStripMenuItem.Enabled = false;
            ChoMuonSachToolStripMenuItem.Enabled = false;
            NhanTraSáchToolStripMenuItem.Enabled = false;
            GhiNhanMatSachToolStripMenuItem.Enabled = false;
            LapPhieuThuTienPhatToolStripMenuItem.Enabled = false;
            ThanhLySachToolStripMenuItem.Enabled = false;
            QuyDinhToolStripMenuItem.Enabled = false;
            loạiĐộcGiảToolStripMenuItem.Enabled = false;
            QuanLyTheLoaiToolStripMenuItem.Enabled = false;
            QuanLyTacGiaToolStripMenuItem.Enabled = false;
            QuanLyNhaXuatBanToolStripMenuItem.Enabled = false;

            //-----Thống Kê-----
            ThongKeSachTheoTheLoaiToolStripMenuItem.Enabled = false;
            ThongKeSachTraTreToolStripMenuItem.Enabled = false;
            ThongKeNoTienPhatToolStripMenuItem.Enabled = false;

            DangXuatToolStripMenuItem.Enabled = false;
        }

        private void DangNhap()
        {
            //----Quản Lý----
            QuanLyNhanVienToolStripMenuItem.Enabled = false;
            QuanLyDocGiaToolStripMenuItem.Enabled = false;
            QuanLySachToolStripMenuItem.Enabled = false;
            ChoMuonSachToolStripMenuItem.Enabled = true;
            NhanTraSáchToolStripMenuItem.Enabled = true;
            GhiNhanMatSachToolStripMenuItem.Enabled = false;
            LapPhieuThuTienPhatToolStripMenuItem.Enabled = false;
            ThanhLySachToolStripMenuItem.Enabled = false;
            QuyDinhToolStripMenuItem.Enabled = true;
            loạiĐộcGiảToolStripMenuItem.Enabled = true;
            QuanLyTheLoaiToolStripMenuItem.Enabled = false;
            QuanLyTacGiaToolStripMenuItem.Enabled = false; ;
            QuanLyNhaXuatBanToolStripMenuItem.Enabled = false;

            //-----Thống Kê-----
            ThongKeSachTheoTheLoaiToolStripMenuItem.Enabled = true;
            ThongKeSachTraTreToolStripMenuItem.Enabled = true;
            ThongKeNoTienPhatToolStripMenuItem.Enabled = true;

            if (ThongTinDangNhapService.tenTatBoPhan == "TT")   //--Thủ Thư--
            {
                QuanLyDocGiaToolStripMenuItem.Enabled = true;
                GhiNhanMatSachToolStripMenuItem.Enabled = true;
            }
            else if (ThongTinDangNhapService.tenTatBoPhan == "TK")   //--Thủ Kho--
            {
                QuanLySachToolStripMenuItem.Enabled = true;
                ThanhLySachToolStripMenuItem.Enabled = true;
                QuanLyTheLoaiToolStripMenuItem.Enabled = true;
                QuanLyTacGiaToolStripMenuItem.Enabled = true; ;
                QuanLyNhaXuatBanToolStripMenuItem.Enabled = true;
            }
            else if (ThongTinDangNhapService.tenTatBoPhan == "TQ")  //--Thủ Quỹ--
            {
                LapPhieuThuTienPhatToolStripMenuItem.Enabled = true;
            }
            else
                QuanLyNhanVienToolStripMenuItem.Enabled = true;
        }        

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text == "Đăng nhập")
            {
                if (txtMaSoNV.Text.Trim() != "")
                {
                    NhanVienDTO nhanvien = NhanVienService.LayThongTinNhanVienTheoMaSoNV(txtMaSoNV.Text);
                    if (nhanvien.MaTheNV == txtMaSoNV.Text)
                    {
                        if (nhanvien.MatKhau == txtMatKhau.Text)
                        {
                            ThongTinDangNhapService.maNV = nhanvien.MaNV;
                            ThongTinDangNhapService.tenNV = nhanvien.HoTen;
                            ThongTinDangNhapService.maBoPhan = nhanvien.BoPhan;
                            ThongTinDangNhapService.tenTatBoPhan = nhanvien.MaTheNV.Substring(0, nhanvien.MaTheNV.Length - 3);

                            grbDangNhap.Text = "Nhân viên";
                            lblMaSoNV.Text = "Họ tên:";
                            txtMaSoNV.ReadOnly = true;
                            //txtMaSoNV.Text = hoTenNV;
                            txtMaSoNV.Text = ThongTinDangNhapService.tenNV;
                            lblMatKhau.Text = "Bộ phận";
                            txtMatKhau.ReadOnly = true;
                            txtMatKhau.Text = ThongTinDangNhapService.tenTatBoPhan;
                            btnDangNhap.Text = "Đăng xuất";

                            MessageBox.Show("Đăng nhập thành công");
                            DangXuatToolStripMenuItem.Enabled = true;
                            DangNhap();

                        }
                        else
                        {
                            MessageBox.Show("Mật khẩu chưa đúng", "Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            txtMatKhau.Select();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Mã số nhân viên chưa đúng", "Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtMaSoNV.Select();
                    }
                }
                else
                {
                    MessageBox.Show("Chưa điền mã số nhân viên", "Đăng Nhập", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaSoNV.Select();
                }
            }
            else
            {
                grbDangNhap.Text = "Đăng nhập";
                lblMaSoNV.Text = "Mã số nhân viên:";
                txtMaSoNV.ReadOnly = false;
                txtMaSoNV.Text = "";
                lblMatKhau.Text = "Mật khẩu:";
                txtMatKhau.ReadOnly = false;
                txtMatKhau.Text = "";
                btnDangNhap.Text = "Đăng nhập";

                ChuaDangNhap();
            }

        }

        private void DangXuatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChuaDangNhap();
        }

        private void DoiMatKhauToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau frm = new frmDoiMatKhau();
            frm.Show();
        }

        private void QuanLyNhanVienToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmQuanLyNhanVien frm = new frmQuanLyNhanVien();
            frm.Show();
        }

        private void QuanLySachToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void ChoMuonSachToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void GhiNhanMatSachToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGhiNhanMatSach frmGhiNhan = new frmGhiNhanMatSach();
            frmGhiNhan.MdiParent = this;
            frmGhiNhan.Show();
        }

        private void LapPhieuThuTienPhatToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void ThanhLySachToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void QuyDinhToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void QuanLyTheLoaiToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void QuanLyTacGiaToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void QuanLyNhaXuatBanToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void TraCuuDocGiaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmTraCuuDocGia frmTraCuuDG = new frmTraCuuDocGia();
            frmTraCuuDG.MdiParent = this;
            frmTraCuuDG.Show();
        }

        private void ThongKeSachTheoTheLoaiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThongKeMuonSach frmTKSachTheoTL = new frmThongKeMuonSach();
            frmTKSachTheoTL.Show();
        }

        private void ThongKeNoTienPhatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThongKeDocGiaNoTienPhat frmDocGiaNoTP = new frmThongKeDocGiaNoTienPhat();
            frmDocGiaNoTP.Show();
        }

        private void ThongKeSachTraTreToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void GioiThieuChuongTrinhToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void HuongDanSuDungToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
